Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: add support for os/signal #4378

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open

runtime: add support for os/signal #4378

wants to merge 1 commit into from

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Aug 2, 2024

This adds support for enabling and listening to signals on Linux and MacOS.

TODO: also support disabling signals.


Not tested on MacOS yet. CI will show whether this works or not.
Also, this will need a few changes to not run the signal test systems that don't support it: Windows, WebAssembly and baremetal.

Copy link

github-actions bot commented Aug 2, 2024

Size difference with the dev branch:

Binary size difference
not the same command!
    tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650
    go: downloading tinygo.org/x/tinyfont v0.3.0
not the same command!
    tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/
    go: downloading golang.org/x/net v0.7.0
not the same command!
    tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/
    go: downloading github.com/soypat/natiu-mqtt v0.5.1
not the same command!
    tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/
    go: downloading github.com/eclipse/paho.mqtt.golang v1.2.0
 flash                          ram
 before   after   diff          before   after   diff
  16820   16820      0   0.00%    4324    4324      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650
  61224   61224      0   0.00%    6196    6196      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
   9528    9528      0   0.00%    4756    4756      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
  13516   13516      0   0.00%    6796    6796      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx
   8640    8640      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
  11712   11712      0   0.00%    6580    6580      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/apds9960/proximity/main.go
   9756    9756      0   0.00%    4768    4768      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
   8332    8332      0   0.00%    2320    2320      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/at24cx/main.go
   8084    8084      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bh1750/main.go
   7392    7392      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/blinkm/main.go
  70408   70408      0   0.00%    3660    3660      0   0.00% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go
  63780   63780      0   0.00%    6196    6196      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
  27380   27380      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp180/main.go
  63832   63832      0   0.00%    6228    6228      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
  12144   12144      0   0.00%    4820    4820      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bmp388/main.go
   8164    8164      0   0.00%    3348    3348      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/sram/main.go
  22084   22084      0   0.00%    3544    3544      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/time/main.go
  69512   69512      0   0.00%    6376    6376      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/main.go
   4580    4580      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/easystepper/main.go
  69104   69104      0   0.00%    6976    6976      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/flash/console/spi
  65492   65492      0   0.00%    9012    9012      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/flash/console/qspi
   7172    7172      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/gc9a01/main.go
  67484   67484      0   0.00%    6368    6368      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/i2c/main.go
  68020   68020      0   0.00%    6504    6504      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go
   7832    7832      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go
   5792    5792      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go
   5744    5744      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go
  10472   10472      0   0.00%    4756    4756      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/hd44780i2c/main.go
  14640   14640      0   0.00%    6580    6580      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/hts221/main.go
  16108   16108      0   0.00%    2360    2360      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hub75/main.go
  10116   10116      0   0.00%    6916    6916      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic
  10604   10604      0   0.00%    4868    4868      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic
  29476   29476      0   0.00%   38076   38076      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing
  10136   10136      0   0.00%    6924    6924      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll
  10692   10692      0   0.00%    4876    4876      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll
 263452  263452      0   0.00%   46760   46760      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/slideshow
  11692   11692      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis3dh/main.go
  13984   13984      0   0.00%    6580    6580      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/lps22hb/main.go
  26092   26092      0   0.00%    2328    2328      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/lsm303agr/main.go
  12408   12408      0   0.00%    4796    4796      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/lsm6ds3/main.go
  10704   10704      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mag3110/main.go
   9880    9880      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017/main.go
  10316   10316      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017-multiple/main.go
   9740    9740      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go
  68408   68408      0   0.00%    6196    6196      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp2515/main.go
  27180   27180      0   0.00%    3648    3648      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go
  26924   26924      0   0.00%    5696    5696      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit-v2 ./examples/microbitmatrix/main.go
   8200    8200      0   0.00%    4756    4756      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mma8653/main.go
   8108    8108      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mpu6050/main.go
  75688   75688      0   0.00%    7480    7480      0   0.00% tinygo build -size short -o ./build/test.hex -target=p1am-100 ./examples/p1am/main.go
  12212   12212      0   0.00%    3352    3352      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/pca9685/main.go
   6188    6188      0   0.00%    3288    3288      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setbuffer/main.go
   5220    5220      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go
  10488   10488      0   0.00%    3328    3328      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/seesaw
   2825    2825      0   0.00%     558     558      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino ./examples/servo
  13728   13728      0   0.00%    3400    3400      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico     ./examples/sgp30
   8044    8044      0   0.00%    6796    6796      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go
  57220   57220      0   0.00%    3692    3692      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
  57276   57276      0   0.00%    3700    3700      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go
  57184   57184      0   0.00%    3692    3692      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
   6612    6612      0   0.00%    2288    2288      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go
   6060    6060      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go
   5808    5808      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go
   6736    6736      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go
   6648    6648      0   0.00%    2280    2280      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go
  16924   16924      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/thermistor/main.go
  10440   10440      0   0.00%    4540    4540      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-bluefruit ./examples/tone
  10028   10028      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/tm1637/main.go
   9492    9492      0   0.00%    6788    6788      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/fourwire/main.go
  12560   12560      0   0.00%    6984    6984      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/pyportal_touchpaint/main.go
  14956   14956      0   0.00%    4756    4756      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl53l1x/main.go
  13432   13432      0   0.00%    4756    4756      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl6180x/main.go
   6452    6452      0   0.00%    2320    2320      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13/main.go
   6140    6140      0   0.00%    2312    2312      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13x/main.go
   6364    6364      0   0.00%    2320    2320      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd4in2/main.go
  26408   26408      0   0.00%   16732   16732      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/waveshare-epd/epd2in66b/main.go
   6808    6808      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/ws2812
   5738    5738      0   0.00%    9538    9538      0   0.00% tinygo build -size short -o ./build/test.bin -target=m5stamp-c3          ./examples/ws2812
  62244   62244      0   0.00%    5952    5952      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/is31fl3731/main.go
   1581    1581      0   0.00%     598     598      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino   ./examples/ws2812
   1056    1056      0   0.00%     180     180      0   0.00% tinygo build -size short -o ./build/test.hex -target=digispark ./examples/ws2812
  31816   31816      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bme280/main.go
  16412   16412      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/microphone/main.go
  11052   11052      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/buzzer/main.go
  12760   12760      0   0.00%    4788    4788      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/veml6070/main.go
   6724    6724      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/simple/main.go
   8636    8636      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/speed/main.go
   6692    6692      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/simple/main.go
   9244    9244      0   0.00%    4756    4756      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/speed/main.go
   7296    7296      0   0.00%    3320    3320      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-f103rb ./examples/shiftregister/main.go
   6980    6980      0   0.00%    2268    2268      0   0.00% tinygo build -size short -o ./build/test.hex -target=hifive1b ./examples/ssd1351/main.go
  12964   12964      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis2mdl/main.go
   8840    8840      0   0.00%    4764    4764      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/max72xx/main.go
  77092   77092      0   0.00%    6344    6344      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/dht/main.go
  36484   36484      0   0.00%    3988    3988      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8523/
  71120   71120      0   0.00%    6344    6344      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/alarm/
   7236    7236      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/clkout/
  70656   70656      0   0.00%    6340    6340      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/time/
  71080   71080      0   0.00%    6352    6352      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/timer/
  12148   12148      0   0.00%    3304    3304      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/qmi8658c/main.go
   8716    8716      0   0.00%    4756    4756      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina260/main.go
   9296    9296      0   0.00%    5260    5260      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-l432kc ./examples/aht20/main.go
  72580   72580      0   0.00%   10748   10748      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/sdcard/console/
  61320   61320      0   0.00%    8232    8232      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/i2csoft/adt7410/
  10260   10260      0   0.00%    6796    6796      0   0.00% tinygo build -size short -o ./build/test.elf -target=wioterminal ./examples/axp192/m5stack-core2-blinky/
   8996    8996      0   0.00%    3276    3276      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/xpt2046/main.go
  13708   13708      0   0.00%    4940    4940      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/sx126x/lora_rxtx/
  31288   31288      0   0.00%    4548    4548      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/ssd1289/main.go
  11224   11224      0   0.00%    4252    4252      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/irremote/main.go
  11872   11872      0   0.00%    3324    3324      0   0.00% tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/uc8151/main.go
  10388   10388      0   0.00%    3356    3356      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/scd4x/main.go
   8512    8512      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go
   9508    9508      0   0.00%    4764    4764      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ds18b20/main.go
  82976   82976      0   0.00%    6588    6588      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/lora/lorawan/atcmd/
  15884   15884      0   0.00%    4880    4880      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/as560x/main.go
   9856    9856      0   0.00%    3296    3296      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu6886/main.go
   7768    7768      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ttp229/main.go
  66648   66648      0   0.00%    4816    4816      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/ndir/main_ndir.go
  62076   62076      0   0.00%    3788    3788      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ndir/main_ndir.go
  65068   65068      0   0.00%    6260    6260      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ndir/main_ndir.go
   9284    9284      0   0.00%    3288    3288      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu9150/main.go
  11364   11364      0   0.00%    3324    3324      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/sh1106/macropad_spi
   8424    8424      0   0.00%    3760    3760      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/encoders/quadrature-interrupt
  65632   65632      0   0.00%    4784    4784      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mcp9808/main.go
  85860   85860      0   0.00%    5140    5140      0   0.00% tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/
 295052  295052      0   0.00%   12760   12760      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal -stack-size 8kb ./examples/net/http-get/
 119240  119240      0   0.00%    7848    7848      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 -stack-size 8kb ./examples/net/tcpclient/
 244184  244184      0   0.00%    9516    9516      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/
 103368  103368      0   0.00%    9832    9832      0   0.00% tinygo build -size short -o ./build/test.hex -target=metro-m4-airlift -stack-size 8kb ./examples/net/socket/
 336920  336920      0   0.00%   15748   15748      0   0.00% tinygo build -size short -o ./build/test.hex -target=matrixportal-m4 -stack-size 8kb ./examples/net/webstatic/
 111444  111444      0   0.00%    7772    7772      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-mkrwifi1010 -stack-size 8kb ./examples/net/tlsclient/
 154088  154088      0   0.00%    6540    6540      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/
 116948  116948      0   0.00%   13132   13132      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webclient/
 284640  284640      0   0.00%   18172   18172      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webserver/
 285736  285736      0   0.00%   17240   17240      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/
5256780 5256780      0   0.00%  784230  784230      0   0.00%

@aykevl
Copy link
Member Author

aykevl commented Aug 2, 2024

Fixed support for MacOS. It needed an implementation of syscall.Kill which was simple to add.

Comment on lines 261 to 315
// Set up a channel to receive signals into.
// A channel size of 1 should be sufficient in most cases, but using 4 just
// to be sure.
signalChan = make(chan uint32, 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on that? Could you tell me what the requirements/context are for this?

var signalChan chan uint32

//go:linkname signal_enable os/signal.signal_enable
func signal_enable(s uint32) {
Copy link
Contributor

@leongross leongross Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a good idea to add the remaining signal functions (signal_disable, signal_ignore, signal_ignored) golang provides as well, or at least stub them out. I remember building applications relying on the signal packages and often requiring most of them.

Also functions like signal.Ignore(.) can easily be added as public API wrappers for the same functionality implemented in the runtime.

@leongross leongross mentioned this pull request Aug 5, 2024
@aykevl
Copy link
Member Author

aykevl commented Aug 8, 2024

I'm working on extending this PR to include all remaining signal features, but it's a bit harder than I expected to do correctly without race conditions.

@leongross
Copy link
Contributor

but it's a bit harder than I expected to do correctly without race conditions

how do you test for that systematically to detect them? Is there already a testing setup for that or did you observe this in manual testing?

@aykevl
Copy link
Member Author

aykevl commented Aug 8, 2024

I don't have tests, I reason through it by reading how POSIX signals work and by knowing how chan works in TinyGo. And I realized that channels are probably not safe. (They are made safe for interrupts on baremetal, but I don't think there's something similar for POSIX signals - I need to check to be sure).

@deadprogram
Copy link
Member

@aykevl please resolve merge conflict... thanks!

@aykevl aykevl marked this pull request as draft August 12, 2024 11:13
@aykevl
Copy link
Member Author

aykevl commented Aug 12, 2024

This is a bit more tricky than I expected, I need to work on it a bit more to make it safe. So it's not just a matter of rebasing.

@leongross
Copy link
Contributor

So if this turns out to be more tricky than initially anticipated it might be a good idea to first merge some stubbed implementations and then take on the more complicated work after. We would like to get this working with u-root by the middle of September and would appreciate some quick prototyping efforts.

@leongross
Copy link
Contributor

@aykevl any thoughts?

@leongross leongross mentioned this pull request Sep 2, 2024
@aykevl aykevl force-pushed the unix-signal branch 3 times, most recently from 246de9c to 2811f2d Compare September 11, 2024 15:38
@aykevl
Copy link
Member Author

aykevl commented Sep 11, 2024

Implemented disabling of signals.
Also fixed some race conditions, but while doing that I found a few others. I'm not entirely sure how to fix those since MacOS does not implement the necessary POSIX function to do that (sigtimedwait), all the existing ones like usleep can't be used in a race-free way as far as I can see. But I've documented all that I know in comments.

@aykevl aykevl marked this pull request as ready for review September 11, 2024 15:39
@aykevl
Copy link
Member Author

aykevl commented Sep 11, 2024

Actually I think I thought of a way to safely use signals on MacOS: by using sigsetjmp and siglongjmp. That's kinda ugly, but as long as sigtimedwait isn't implemented that might be the most reasonable way to do it.


// Just stubbing the functions for now since signal handling is not yet implemented in tinygo
func Reset(sig ...os.Signal) {}
func Ignore(sig ...os.Signal) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason you want to remove these functions? They are widely used and we need an API to interact with the runtime. I proposed a fairly basic implementation for that in #4384, maybe we can do something similar here?

Suggested change
func Ignore(sig ...os.Signal) {}
func Ignore(sig ...os.Signal) {
for _, s := range sig {
signal_ignore(uint32(signum(s)))
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using our own implementation, we use the one in the Go standard library. That one is already well tested so as long as the runtime side is implemented correctly, the signal package should also be correct.

By removing the package entirely, loader/goroot.go will pick the one from the standard library.

func signal_enable(s uint32) {
if s >= 32 {
// TODO: to support higher signal numbers, we need to turn
// receivedSignals into a uint32 array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to do it this way uint64 might even be sufficient.
checking the values for SIGRTMAX on my system leaves me with 64. From a quick online look up is seems to be the case for many linux systems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, uint64 is not atomic on some systems such as MIPS32.
But signal numbers higher than 31 are rarely used (I think), so we can leave that for now and implement it when someone needs it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense. I also had a deeper look into the standard signals on signal (7) and indeed it looks like 1-31 are the common signals which end at SIGRTMIN. So for now there are only 2 entirely user-definable signals available (SIGUSR1, SIGUSR2) but for basic signal support, this should suffice.

// Stop notifying.
// (This is just a smoke test, it's difficult to test the default behavior
// in a unit test).
signal.Ignore(syscall.SIGUSR1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused here, I thought the go API was removed by removing signal.go

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's replaced by the standard library package, see my other comment.

func signal_enable(s uint32) {
if s >= 32 {
// TODO: to support higher signal numbers, we need to turn
// receivedSignals into a uint32 array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense. I also had a deeper look into the standard signals on signal (7) and indeed it looks like 1-31 are the common signals which end at SIGRTMIN. So for now there are only 2 entirely user-definable signals available (SIGUSR1, SIGUSR2) but for basic signal support, this should suffice.

}
tinygo_signal_disable(s)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to add signal_ignored. This could implemented via a bitmap as well.

Suggested change
func signal_ignored(s uint32)

Copy link
Member Author

@aykevl aykevl Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a code sample that requires this?
The test includes signal.Ignore(...) call, which doesn't result in signal_ignored being called.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had something in the back of my mind but I had a look at my build logs and could not find it, so let's leave it for now.

@leongross
Copy link
Contributor

@aykevl I think the CI failure is just racy, maybe re-run it? Also, is there anything I can further help on?

@aykevl
Copy link
Member Author

aykevl commented Oct 2, 2024

Yeah that's an unrelated failure. TinyHCI does fail sometimes for no apparent reason.

Comment on lines 232 to 234
// TODO: there is a race condition here. If a signal arrives between
// checkSignals() and usleep(), the usleep() call will not exit early so the
// signal is delayed until usleep finishes or another signal arrives.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iirc we talked about this issue and the only way to get this working asynchronously is by adding a second listening thread/go routine which is not possible at the moment, right? So for the initial support, we should not see this as a blocking factor.

Comment on lines +457 to +488
// TODO: use atomic.Uint32.Or once we drop support for Go 1.22 instead
// of this loop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Go 1.22 support will not be dropped in the next view releases, we should also not consider this a blocking factor here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, all it will do is make the code look better (and maybe a bit faster though that's probably not noticeable). I intentionally put in "Go 1.22" because when dropping support for a Go version I typically will grep for such strings to fix cases like these.

Comment on lines +407 to +438
// TODO: use atomic.Uint32.And once we drop support for Go 1.22 instead of
// this loop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Go 1.22 support will not be dropped in the next view releases, we should also not consider this a blocking factor here.

Same here

// Atomically find a signal that previously occured and send it into the
// signalChan channel. Return true if at least one signal was delivered this
// way, false otherwise.
func checkSignals() bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the return code for checkSignals has not been really checked. Is this on purpose or by accident?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used in sleepTicks, where it is used to exit early before sleeping.

@leongross
Copy link
Contributor

I have trouble running the tests locally, the test always fails with a segfault. This does not look to me as the mentioned race condition:

$ tinygo test os/signal 
FAIL    os/signal       0.000s
# internal/testpty
../lib/musl/include/fcntl.h:22:10: fatal: 'bits/fcntl.h' file not found
/usr/local/go/src/internal/testpty/pty_cgo.go:11:10: note: in file included from pty_cgo.go!cgo.c:4:
$ tinygo run testdata/signal.go
panic: runtime error at 0x0000000000000007: caught signal SIGSEGV

@leongross
Copy link
Contributor

What do you think about #4508 ?

@aykevl aykevl force-pushed the unix-signal branch 2 times, most recently from db37512 to aa061fb Compare October 10, 2024 10:31
This adds support for enabling and listening to signals on Linux and
MacOS.
@aykevl
Copy link
Member Author

aykevl commented Oct 10, 2024

I have updated the PR and made signals non-racy on Linux (I hope!).

  • Using sigtimedwait on Linux for sleeping. Fall back to a racy usleep on MacOS (there's not much we can do about that).
  • Using sigwait in waitForEvents. While sigsuspend would work just fine, sigwait is more similar to sigtimedwait and so keeps the code hopefully a bit easier to follow.

Apart from some minor things your PR looked good, but I wanted to make sure the other race condition was also fixed at the same time.

@leongross
Copy link
Contributor

@aykevl nix-ci seems to be broken due to llvm on other branches as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants